import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';

class LogoutOneView extends StatefulWidget {
  const LogoutOneView({super.key});

  @override
  State<LogoutOneView> createState() => LogoutOneViewState();
}

class LogoutOneViewState extends State<LogoutOneView> {
  int selectedIndex = -1;
  final TextEditingController _reasonController = TextEditingController();
  List<String> getReasons(BuildContext context) => [
    AppLocalizations.of(context)!.securityPrivacyConcern,
    AppLocalizations.of(context)!.redundantAccount,
    AppLocalizations.of(context)!.shoppingDifficulty,
    AppLocalizations.of(context)!.cannotChangeUsername,
    AppLocalizations.of(context)!.otherReason
  ];

  bool get isValidSelection {
    if (selectedIndex == -1) return false;
    if (selectedIndex == getReasons(context).length - 1) {
      return _reasonController.text.trim().isNotEmpty;
    }
    return true;
  }

  TextEditingController get reasonController => _reasonController;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: [
          Container(
            width: double.infinity,
            height: 50,
            color: const Color(0xFFF5F5F5),
            padding: const EdgeInsets.only(left: 20),
            alignment: Alignment.centerLeft,
            child: Text(
              AppLocalizations.of(context)!.selectLogoutReason,
              style: TextStyle(
                fontSize: 15,
                color: Color(0xFF505050),
              ),
            ),
          ),
          ...List.generate(getReasons(context).length, (index) {
            return ListTile(
              contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 20),
              title: Text(
                getReasons(context)[index],
                style: TextStyle(
                  fontSize: 15,
                  color: Color(0xFF505050),
                ),
              ),
              trailing: Icon(
                selectedIndex == index
                    ? Icons.radio_button_checked
                    : Icons.radio_button_off,
                color: selectedIndex == index ? Colors.blue : Colors.grey,
              ),
              onTap: () {
                setState(() {
                  selectedIndex = index;
                });
              },
            );
          }),
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 25, vertical: 5),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5),
              border: Border.all(color: Color(0xFFF0F0F0)),
            ),
            padding: const EdgeInsets.all(15),
            child: TextField(
              controller: _reasonController,
              maxLines: 3,
              decoration: InputDecoration(
                hintText: AppLocalizations.of(context)!.describeOtherReason,
                border: InputBorder.none,
                hintStyle: TextStyle(
                  fontSize: 15,
                  color: Color(0xFF666666),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}